<?php

/**
 * +------ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER * ---------+
 * +----------------------------------------------------------------------------+
 * | PHPKiisf - Keep It in Simple Framework					|
 * | Version 1.0								|
 * | Copyright (C) <2013>  Mario Cezzare <mcezzare@gmail.com>			|
 * | Blog: http://mcezzare.blogspot.com.br					|
 * |										|
 * | This program is part of PHPKiisf, and is free software: you can		|
 * | redistribute it and/or modify it under the terms of the GNU 		|
 * | General Public License as published by					|
 * | the Free Software Foundation, either version 3 of the License, or		|
 * | (at your option) any later version.					|					|
 * |										|									|
 * | This program is distributed in the hope that it will be useful,		|
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of		|
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		|
 * | GNU General Public License for more details.				|
 * | You should have received a copy of the GNU General Public License		|
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.	|
 * +----------------------------------------------------------------------------+
 * | Author : <Mario Cezzare Angelicola Chiodi> <mcezzare@gmail.com>		|
 * +----------------------------------------------------------------------------+
 * | Contributors								|
 * |										|
 * +----------------------------------------------------------------------------+
 *
 */
/*
 * @package	Controllers
 * @access	public
 * @since	2013-03-08
 * @author	Mario Cezzare <mcezzare@gmail.com>
 */

/**
 * Base controller class of framework.
 *
 * Others must extend this
 *
 */
class default_controller {

	protected static $_x ;
	public static $_info = array() ;
	public static $_PREFIX_DEBUG_MESSAGE ;

	/**
	 *
	 */
	public function __construct() {
		self::$_PREFIX_DEBUG_MESSAGE = sprintf( "\n[%s] - " , ucfirst( ( __CLASS__ ) ) ) ;
	}

	/**
	 *
	 */
	public static function init() {
		self::$_info = array(
			"dir" => __DIR__ ,
			"caller" => get_called_class() ,
			) ;
		self::$_PREFIX_DEBUG_MESSAGE = sprintf( "\n[%s] - " , ucfirst( ( __CLASS__ ) ) ) ;
	}

	/**
	 *
	 * @param type $name
	 * @return type
	 */
	public function __get( $name ) {
		if ( array_key_exists( $name , $this ) ) {
			return $this->$name ;
		}
	}

	/**
	 *
	 * @param type $name
	 * @param type $value
	 */
	public function __set( $name , $value ) {
		if ( array_key_exists( $name , $this ) ) {
			$this->$name = $value ;
		}
	}

	/**
	 * Include the default template
	 *
	 * @param Array $options , some useful params to control
	 * the default template behavior
	 * @return boolean
	 */
	public static function display_index( $options = array() ) {
		display_user_message( sprintf( "Hello from : %s <br>" , __CLASS__ ) ) ;

		if ( count( $options ) ) {
			if ( in_array( "ERR" , $options ) ) {
				utils::show_object( $options ) ;
			} else {
				utils::show_object( $options ) ;
			}
		}
		return true ;
	}

	/**
	 *
	 * @param type $options
	 * @return boolean
	 *
	 */
	public static function _display_index( $options = array() ) {
		display_user_message( sprintf( "Hello from : %s <br>" , get_class( self ) ) ) ;

		if ( count( $options ) ) {
			print_r( $options ) ;
		}
		return true ;
	}

	/**
	 * To get the template for each extended class
	 *
	 * @param String $template_name
	 * @return String
	 */
	public static function get_template( $template_name ) {
		/* to get templates list from ini */

		$template_list = config::get_config( "TEMPLATES" ) ;
		$include_file = $template_list{$template_name} ;
		return $include_file ;
	}

	/**
	 *
	 * @param type $include_file
	 * @param type $section_name
	 * @return boolean
	 */
	public static function include_template( $include_file , $params = null ) {

		/* If exists include the template, defined in phpkiisf.ini */

		if ( ($params !== null) && (is_array( $params )) ) {
//preserve original params
			array_push( base_template::$global_params , $params ) ;
			/* check for alternative file to load.
			 * a simple key=>value alternative_file=>file_to_load
			 * defined in $param
			 */
			$alternative_file_key = "alternative_file" ;
			$section_name = "section_name" ;
			if ( array_key_exists( $section_name , $params ) && $params{$section_name} ) {
				base_template::$section_name = $params{$section_name} ;
			}
			if ( array_key_exists( $alternative_file_key , $params ) && $params{$alternative_file_key} ) {
				$final_include = TEMPLATES_DIR . $params{$alternative_file_key} . INCLUDE_EXTENSION ;
				if ( utils::check_template( $final_include ) ) {
					require_once $final_include ;
					return true ;
				}
			}
		} else {
			base_template::$global_params = $params ;
		}


		if ( utils::check_template( $include_file ) ) {

			require_once ($include_file) ;
			return true ;
		} else {
			utils::inform_user(
				array(
					"MSG_TYPE" => "ERROR" ,
					"OPERATION" => "INDEX_CONFIG NOT_FOUND" ,
					"DETAIL" => "FILE TO INCLUDE: " . var_export( $include_file ) ,
					"OBS" => utils::object_to_string( error_get_last() ) ,
				)
			) ;
			return false ;
		}
	}

	/**
	 *
	 * @return boolean
	 */
	public function display_test() {
		display_user_message( sprintf( "Hello from : %s <br>" , get_class( $this ) ) ) ;
		return true ;
	}

	/**
	 *
	 * @param type $options
	 * @return boolean
	 */
	public function display_test_new( $options = array() ) {
		display_user_message( "This is a test from  <br>" . utils::show_object( get_class_methods( $this ) ) ) ;
		display_user_message( sprintf( "Hello from : %s <br>" , get_class( $this ) ) ) ;
		utils::show_object( $options ) ;
		return true ;
	}

	/**
	 *
	 * @param type $options
	 * @return boolean
	 */
	public function display_test_edit( $options = array() ) {

		print_r( $options ) ;
		return true ;
	}

}

?>